﻿/**************************************************************************
创建时间:	2021/1/25 0:25:53    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
    2021.12.9 增加日期范围
***************************************************************************/
using System;

namespace ZhCun.Utils
{
    /// <summary>
    /// 时间类型枚举
    /// </summary>
    public enum EmTimeUnit : int
    {
        /// <summary>
        /// 年
        /// </summary>
        yy = 1,
        /// <summary>
        /// 月
        /// </summary>
        MM = 2,
        /// <summary>
        /// 日
        /// </summary>
        dd = 3,
        /// <summary>
        /// 时
        /// </summary>
        HH = 4,
        /// <summary>
        /// 分
        /// </summary>
        mm = 5,
        /// <summary>
        /// 秒
        /// </summary>
        ss = 6
    }
    /// <summary>
    /// 时间备注
    /// </summary>
    public class TimeRemark
    {
        public TimeRemark() { }

        public TimeRemark(string remark, int add, EmTimeUnit addUnit)
        {
            Remark = remark;
            AddCount = add;
            AddUnit = addUnit;
        }

        /// <summary>
        /// 备注说明
        /// </summary>
        public string Remark { set; get; }
        /// <summary>
        /// 增加指定单位的数量，负数为减
        /// </summary>
        public int AddCount { set; get; }
        /// <summary>
        /// 增加单位，1:秒，2：分，3：时，4：天，5：月，6：年
        /// </summary>
        public EmTimeUnit AddUnit { set; get; }
        /// <summary>
        /// 返回时间范围
        /// </summary>
        public DateTime TimeValue
        {
            get
            {
                return GetTimeValue();
            }
        }

        DateTime GetTimeValue()
        {
            switch (AddUnit)
            {
                case EmTimeUnit.yy:
                    return DateTime.Now.AddMonths(12 * AddCount);
                case EmTimeUnit.MM:
                    return DateTime.Now.AddMonths(AddCount);
                case EmTimeUnit.dd:
                    return DateTime.Now.AddDays(AddCount);
                case EmTimeUnit.HH:
                    return DateTime.Now.AddHours(AddCount);
                case EmTimeUnit.mm:
                    return DateTime.Now.AddMinutes(AddCount);
                case EmTimeUnit.ss:
                    return DateTime.Now.AddSeconds(AddCount);
                default:
                    return DateTime.Now;
            }
        }

        public override string ToString()
        {
            return Remark;
        }
    }

    /// <summary>
    /// 日期范围, 
    /// </summary>
    public class DateRange
    {
        public DateRange(DateTime startDate, string caption)
        {
            StartDate = startDate;
            EndDate = DateTime.Now;
            Caption = caption;
        }

        public DateRange(int days, string caption)
        {
            StartDate = DateTime.Now.AddDays(days * -1).Date;
            EndDate = DateTime.Now.Date;
            Caption = caption;
        }

        public DateRange(string caption)
        {
            IsValid = false;
            Caption = caption;
        }

        /// <summary>
        /// 返回开始日期(已转 Date)
        /// </summary>
        public DateTime StartDate { private set; get; }
        /// <summary>
        /// 返回结束日期(已转 Date)
        /// </summary>
        public DateTime EndDate { private set; get; }

        public string Caption { private set; get; }
        /// <summary>
        /// 当为false 时不使用该返回对象(自定义)
        /// </summary>
        public bool IsValid { private set; get; } = true;

        public override string ToString()
        {
            return Caption;
        }
    }
}